<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJAFieldList extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct( $location = array() )
    {    	
        parent::__construct( $location ); 
        checkPermission_Administrator('fieldlist');     
        // Register Extra tasks
        $this->registerTask( 'add',     'edit' );
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'unpublish', 	'publish');
		$this->registerTask( 'orderup', 	'orderdown');
    }
    
    /**
    * Display current jafieldlist of the component to administrator
    * 
    */
    function display(){
    	
    	switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', false );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jafieldlist');
				$model->checkout();
			} break;
			case 'edit':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jafieldlist');
				$model->checkout();
			} break;
		}
		
		parent::display();
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jafieldlist");
    }   
    /**
    * Remove a jafieldlist row
    * 
    */   
    function remove(){
    	global $option;
 
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $model =& $this->getModel('jafieldlist');
        if(($n = $model->remove()) < 0){
             JError::raiseWarning( 500, $row->getError() );
		}
		
		$this->setMessage(JText::_('Successfully removed item(s)'));
		$this->setRedirect("index.php?option=$option&view=jafieldlist");
	}
	
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		$cache = & JFactory::getCache($option);
		$cache->clean();

		$model	=& $this->getModel('jafieldlist');
		$post	= JRequest::get('post');
		
		$model->setState( 'request', $post );
		
		$msg = '';
		if ($id = $model->store()) {
			if ((isset($cid[0]))&&($cid[0]!=0))
				$msg = JText::_( 'Sucessful updated column' );
			else 
				$msg = JText::_( 'Sucessful created column' );
		} else {
			JError::raiseWarning(1, JText::_( 'Please select column' ));
			
		}
		
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jafieldlist&task=edit&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jafieldlist", $msg );
				break;
		}
	}	
    
    /**
    * Move down the order of field list item
    * 
    */
    function orderdown(){
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);
        
        $task = JRequest::getVar('task', 'orderdown');

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->setRedirect( "index.php?option=$option&view=jafieldlist", JText::_('No Items Selected') );
            return false;
        }

        $model =& $this->getModel( 'jafieldlist' );
        $movement = $task == 'orderdown' ? 1 : -1;

        if($model->orderItem($id, $movement)){
            $msg = $movement > 0 ? JText::_( 'Item Moved Down' ) : JText::_( 'Item Moved Up' );
        }
        
        $this->setRedirect( "index.php?option=$option&view=jafieldlist", $msg );
    }
    
    /**
    * Save the item(s) order
    */
    function saveorder()
    {
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);

        $model =& $this->getModel( 'jafieldlist' );
        if ($model->setOrder($cid)) {
            $msg = JText::_( 'New ordering saved' );
        } else {
            $msg = $model->getError();
        }
        $this->setRedirect(  "index.php?option=$option&view=jafieldlist", $msg );
    }
}